package com.examstack.portal.controller.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.examstack.common.Constants;
import com.examstack.common.domain.exam.Message;
import com.examstack.common.domain.exam.UserQuestionCollect;
import com.examstack.common.domain.practice.Collect;
import com.examstack.common.domain.question.QuestionHistory;
import com.examstack.portal.security.UserInfo;
import com.examstack.portal.service.QuestionCollectService;

@Controller
public class CollectAction {

	@Autowired
	private QuestionCollectService questionCollectService;

	/**
	 * 试题收藏
	 * 
	 * @return
	 */
	@RequestMapping(value = "/student/question-collect", method = RequestMethod.POST)
	public @ResponseBody Message CollectQuestion(@RequestBody QuestionHistory qh) {
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Message msg = new Message();

		/*
		 * Collect co = new Collect(); co.setQuestion_id(qh.getQuestionId());
		 * co.setUser_id(userInfo.getUserid());
		 * co.setQuestion_type_id(qh.getQuestionTypeId());
		 */

		UserQuestionCollect collect = new UserQuestionCollect();
		collect.setQuestionId(qh.getQuestionId());
		collect.setUserId(userInfo.getUserid());
		collect.setQuestionTypeId(qh.getQuestionTypeId());

		try {
			Collect co = questionCollectService.Collect(userInfo.getUserid(), qh.getQuestionId());
			// 题目在收藏表中不存在
			if (co == null ) {
				questionCollectService.addUserQuestionCollect(collect);
			}else if(Constants.COLLECT_FALSE.equals(co.getIsCollect())){
				questionCollectService.updUserCollect(userInfo.getUserid(), qh.getQuestionId(), Constants.COLLECT_TRUE);
			}
			/*
			 * else{
			 * msg.setResult("err"); msg.setMessageInfo("不能重复收藏");
			 * 
			 * }
			 */
		} catch (Exception e) {
			msg.setResult(e.getClass().getName());
			e.printStackTrace();
		}

		return msg;

	}

	/**
	 * 试题是否已收藏
	 * 
	 * @return
	 */
	@RequestMapping(value = "/student/question-collect-is", method = RequestMethod.GET)
	public @ResponseBody Message isCollectQuestion() {

		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		Message msg = new Message();

		Collect co = null;

		int questionid = 1;

		try {
			co = questionCollectService.Collect(userInfo.getUserid(), questionid);
		} catch (Exception e) {
			msg.setResult(e.getClass().getName());
			e.printStackTrace();
		}

		if (co != null && "1".equals(co.getIsCollect())) {
			msg.setObject(co);
			msg.setResult("err");
		}

		return msg;

	}

	/**
	 * 取消收藏
	 * 
	 * @return
	 */
	@RequestMapping(value = "/student/question-collect-un", method = RequestMethod.POST)
	public @ResponseBody Message UnCollectQuestion(@RequestBody QuestionHistory qh) {

		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		Message msg = new Message();

		int questionid = 1;
		try {
			questionCollectService.updUserCollect(userInfo.getUserid(), qh.getQuestionId(), Constants.COLLECT_FALSE);
		} catch (Exception e) {
			msg.setResult(e.getClass().getName());
			e.printStackTrace();
		}

		return msg;

	}
	/**
	 * 练习模式完成一道题
	 * 
	 * @param sp
	 * @return
	 */
	/*
	 * @RequestMapping(value = "/student/practice-improve", method =
	 * RequestMethod.POST) public @ResponseBody Message
	 * submitPractice(@RequestBody QuestionHistory qh) { Message msg = new
	 * Message(); UserInfo userInfo = (UserInfo)
	 * SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	 * 
	 * 
	 * UserQuestionHistory history = new UserQuestionHistory();
	 * history.setQuestionId(qh.getQuestionId());
	 * history.setUserId(userInfo.getUserid());
	 * history.setQuestionTypeId(qh.getQuestionTypeId()); boolean isRight =
	 * qh.getAnswer().equals(qh.getMyAnswer()) ? true : false;
	 * history.setRight(isRight);
	 * 
	 * try { questionHistoryService.addUserQuestionHist(history); } catch
	 * (Exception e) { // TODO Auto-generated catch block
	 * msg.setResult(e.getClass().getName()); e.printStackTrace(); }
	 * 
	 * return msg; }
	 */
}
